<?php
/**
 * 参数列表模型
 * 
 * @version $Id: Params.php 264 2012-04-13 03:06:48Z linsir123 $
 */
class modelParams
{
	/**
	 * @var 参数列表
	 */
	public $data = array();
	
	/**
	 * @var 对应的淘宝客参数
	 */
	public $taobaoke;
	
	/**
	 * @var 列表的拼音
	 */
	private $_pinyin;
	
	/**
	 * @var 排序说明
	 */
	private $_sorts;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$keys = array(
			'p' => 'intval',
			'cid' => 'intval',
			'sort' => 'intval',
			'nick' => 'trim',
			'keyword' => 'trim',
			'start_price' => 'floatval',
			'end_price' => 'floatval'
		);
		foreach ($keys as $k => $m) {
			$var = call_user_func_array($m, array($_GET[$k]));
			if ( ! empty($var)) 
				$this->data[$k] = $var;
		}
		
		///
		$list = array('search', 'zhe');
		if (isset($_GET['pinyin']) && in_array($_GET['pinyin'], $list)) 
			$this->_pinyin = trim($_GET['pinyin']);
			
		///
		if (isset($this->data['nick'])) {
			$tmp = iconv("UTF-8", "UTF-8", $this->data['nick']);
			if ($tmp != $this->data['nick']) 
				$this->data['nick'] = iconv("GBK", "UTF-8", $this->data['nick']);
		}
		
		///
		$this->_initSort(isset($this->data['nick'])?'shop':null);
		$this->_initTobaoke();
	}
	
	/**
	 * 构造查询链接
	 */
	public function createUrl($params = array())
	{
		! empty($params) or $params = $this->data;
		
		///
		if ( ! isset($params['nick'])) {
			if ( ! empty($this->_pinyin)) 
				$params['pinyin'] = $this->_pinyin;
				
			return AppView::urlClass($params);
		} else 
			return AppView::urlShop($params);
	}
	
	/**
	 * 取得分页ID
	 */
	public function getP()
	{
		return isset($this->data['p']) ? $this->data['p'] : 1;
	}
	
	/**
	 * 取得CID
	 */
	public function getCid()
	{
		return isset($this->data['cid']) ? $this->data['cid'] : 0;
	}
	
	/**
	 * 取得NICK
	 */
	public function getNick()
	{
		return isset($this->data['nick']) ? $this->data['nick'] : 0;
	}
	
	/**
	 * 取得关键字
	 */
	public function getKeyword($default = null)
	{
		$keyword = isset($this->data['keyword']) ? $this->data['keyword'] : '';
		if (empty($keyword) && ! empty($default)) 
			$this->data['keyword'] = $this->taobaoke['keyword'] 
				= $keyword = $default;
			
		return $keyword;
	}
	
	/**
	 * 取得排序
	 */
	public function getSort()
	{
		return isset($this->taobaoke['sort']) ? $this->taobaoke['sort'] : '';
	}
	
	/**
	 * 取得体格区间
	 */
	public function getPrice()
	{
		
	}
	
	/**
	 * 取得拼音
	 */
	public function getPinyin()
	{
		return $this->_pinyin;
	}
	
	/**
	 * 转换到视图层
	 */
	public function assign($title, $totalPage, $category = null)
	{
		if ($keyword = $this->getKeyword()) {
			$keyword = htmlspecialchars($keyword);
			$title = $keyword.(empty($title)?'':'_').$title;
		}
		
		///
		if ( ! empty($category)) 
			$title .= (empty($title)?'':'_').$category['title'];
			
		///
		$p = $this->getP();
		if ($p > 1) 
			$title .= '(第'.$p.'页)';
			
		///
		View::assignSet(array(
			'sortInfo' => $this->_aSortInfo(), 
			'searchUrl' => $this->_aSearchUrl(),
			'pagination' => $this->_aPagination($totalPage),
			'keyword' => $keyword, 
			'title' => $title, 
		));
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 初始化淘宝客参数
	 */
	private function _initTobaoke()
	{
		$params = array('show'=>21);
		$keys = array('p', 'cid', 'keyword', 'start_price', 'end_price');
		foreach ($keys as $k) {
			if (isset($this->data[$k])) 
				$params[$k] = $this->data[$k];
		}
		
		///
		if (isset($this->data['nick'])) 
			$params['nicks'] = $this->data['nick'];
			
		///
		$i = isset($this->data['sort']) ? $this->data['sort'] : 0;
		$tmp = isset($this->_sorts[$i]) ? $this->_sorts[$i] : $this->_sorts[0];
		$params['sort'] = $tmp[1];
		
		///
		$this->taobaoke = $params;
	}
	
	/**
	 * 初如化排序信息
	 */
	private function _initSort($type = null)
	{
		if ($type == 'shop') 
			$this->_sorts = array(
				array('热销排行', 'volume:desc'),
				array('人气推荐', 'popularity:desc'),
				array('价格由高至低', 'price:desc'),
				array('价格从低到高', 'price:asc')
			);
		else {
			if ($this->_pinyin == 'zhe') 
				$this->_sorts = array(
					array('默认', ''),
					array('销量', 'sale-desc'),
					array('信用', 'credit-desc')
				);
			else 
				$this->_sorts = array(
					array('热销排行', 'default'),
					array('人气推荐', 'commissionNum_desc'),
					array('店铺掌柜推荐', 'commissionVolume_desc'),
					array('信用由高至低', 'credit_desc'),
					array('价格从低到高', 'price_asc')
				);
		}
	}
	
	/**
	 * 转换成搜索链接
	 */
	private function _aSearchUrl()
	{
		$params = $this->data;
		$params['p'] = '1';
		$params['keyword'] = '__keyword__';
		$params['start_price'] = '__start__';
		$params['end_price'] = '__end__';
		return $this->createUrl($params);
	}
	
	/**
	 * 排序信息
	 */
	private function _aSortInfo()
	{
		$params = $this->data;
		$params['sort'] = '%d';
		$params['p'] = 1;
		$format = $this->createUrl($params);
		
		///
		$str = '';
		foreach ($this->_sorts as $i => $var) {
			list($text, $sort) = $var;
			if ($this->data['sort'] == $i) {
				$class = 'class="this"';
				$href = '#';
			} else {
				$class = '';
				$href = sprintf($format, $i);
			}
			
			$str .= '<li '.$class.'><a href="'.$href.'">'.$text.'</a></li>';
		}
		
		return $str;
	}
	
	/**
	 * 分页
	 */
	private function _aPagination($totalPage)
	{
		$params = $this->data;
		$params['p'] = '%d';
		$url = $this->createUrl($params);
		$com = App::C('Pagination', $url, $this->getP(), $totalPage, 8, array(
			'pre' =>'上一页', 
			'next' =>'下一页'));
			
		return $com->display();
	}
}
